﻿using FCDB.Entity;
using FCDB.Entity.EntityEx;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity.Core;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;

namespace FCDB.Repository
{
    public class FormRepository : LogicBase, ILogic
    {
        public virtual T GetForm<T>(long AppInstanceID)
        {
            Type t = typeof(T);
            try
            {
                var context = ((IObjectContextAdapter)DBHelper).ObjectContext;
                string str = string.Format("select * from {0}   WHERE AppInstanceID={1}", t.Name, AppInstanceID);
                ObjectResult<T> query = context.ExecuteStoreQuery<T>(str);
                IList<T> list = query.ToList();
                if (list.Count() > 0)
                {
                    return list.FirstOrDefault();
                }
                else
                {
                    return default(T);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<FormAttachment> GetFormAttachments(string FormType, long formID)
        {
            return DBHelper.FormAttachment.Where(t => t.FormType == FormType && t.FormID == formID).ToList();
        }
        public IList<FormsRelation> GetFormsRelationsByProcess(Guid ProcessGuid, long AppinstanceID)
        {
            return DBHelper.FormsRelation.Where(t => t.AppInstanceID == AppinstanceID && t.ProcessGUID == ProcessGuid).ToList();
        }
        public IList<FormsRelation> GetFormsRelationsByForm(long formID, string FormTable)
        {
            return DBHelper.FormsRelation.Where(t => t.FormID == formID && t.FormTable == FormTable).ToList();
        }

        public IList<FormCirculatedDocumentLog> GetDocumentLogs(Guid ProcessGUID, long AppInstanceID)
        {
            return DBHelper.FormCirculatedDocumentLog.Where(t => t.ProcessGUID == ProcessGUID && t.AppInstanceID == AppInstanceID).ToList();
        }
        public IList<FormCheckLog> GetFormCheckLogs(long AppInstanceID)
        {
            return DBHelper.FormCheckLog.Where(t => t.AppInstanceID == AppInstanceID).ToList();

        }
        #region 表单组合
        public IList<FormDraft> GetFormDrafts(int skip, int take, Guid userID, out int total, string dtStart = "", string dtEnd = "",
            string kewyword = "")
          {
              int draftStatus = (int)FormStatusEnum.Draft;
              List<FormDraft> list = new List<FormDraft>();
              var query = (
               from item in DBHelper.FormNoContractPay
               where item.CreateUserID == userID && item.Status == draftStatus
               select new FormDraft()
               {
                   CreateTime = (DateTime)item.CreateTime,
                   CreateUserID = item.CreateUserID,
                   ID = item.ID,
                   FormType = "联保保证金流程表",
                   FormAction = "FormNoContractPay",
                   ProccessGuid = item.ProcessGuid
               });
              if (!string.IsNullOrEmpty(dtStart) && !string.IsNullOrEmpty(dtEnd))
              {
                  DateTime dtStartTime = DateTime.Parse(dtStart);
                  DateTime dtEndTime = DateTime.Parse(dtEnd);
                  query = query.Where(t => t.CreateTime >= dtStartTime && t.CreateTime <= dtEndTime);
              }
              if (!string.IsNullOrEmpty(kewyword))
              {
                  query = query.Where(t => t.FormType.IndexOf(kewyword) > -1);
              }
              list.AddRange(query.ToList());
              query = (
                from item in DBHelper.FormCDB
                where item.CreateUserID == userID && item.Status == draftStatus
                select new FormDraft()
                {
                    CreateTime = (DateTime)item.CreateTime,
                    CreateUserID = item.CreateUserID,
                    ID = item.ID,
                    FormType = "诉讼业务申请表",
                    FormAction = "FormCDB",
                    ProccessGuid = item.ProcessGuid
                });
              if (!string.IsNullOrEmpty(dtStart) && !string.IsNullOrEmpty(dtEnd))
              {
                  DateTime dtStartTime = DateTime.Parse(dtStart);
                  DateTime dtEndTime = DateTime.Parse(dtEnd);
                  query = query.Where(t => t.CreateTime >= dtStartTime && t.CreateTime <= dtEndTime);
              }
              if (!string.IsNullOrEmpty(kewyword))
              {
                  query = query.Where(t => t.FormType.IndexOf(kewyword) > -1);
              }
              list.AddRange(query.ToList());
              query = (
                  from item in DBHelper.FormCost
                  where item.CreateUserID == userID && item.Status == draftStatus
                  select new FormDraft()
                  {
                      CreateTime = (DateTime)item.CreateTime,
                      CreateUserID = item.CreateUserID,
                      ID = item.ID,
                      FormType = "担保业务退款表",
                      FormAction = "FormCost",
                      ProccessGuid = item.ProcessGuid
                  });
              if (!string.IsNullOrEmpty(dtStart) && !string.IsNullOrEmpty(dtEnd))
              {
                  DateTime dtStartTime = DateTime.Parse(dtStart);
                  DateTime dtEndTime = DateTime.Parse(dtEnd);
                  query = query.Where(t => t.CreateTime >= dtStartTime && t.CreateTime <= dtEndTime);
              }
              if (!string.IsNullOrEmpty(kewyword))
              {
                  query = query.Where(t => t.FormType.IndexOf(kewyword) > -1);
              }
              list.AddRange(query.ToList());

              total = list.Count;
              list = list.OrderByDescending(t => t.CreateTime).Skip(skip).Take(take).ToList();
              return list;
            
          }

        public int getTodayCount()
        {
            String timeStr = DateTime.Now.ToString("yyyy-MM-dd");
             DateTime dt = DateTime.ParseExact(timeStr, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture);
            int count = DBHelper.FormCDB.Where(t => t.CreateTime > dt).Count();
            return count;
        }
        #endregion

    
        }
}
